﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using XDesigner.AppFramework.DOM;
using XDesigner.AppFramework;
using XDesigner.ORM;

namespace XDesigner.AppFramework.WinForms
{
    public partial class AppCategoryEditControl : UserControl
    {
        public static Form CreateContainerForm( AppFrameworkClass frame )
        {
            Form frm = new Form();
            AppCategoryEditControl ctl = new AppCategoryEditControl();
            ctl.Framework = frame;
            ctl.Dock = DockStyle.Fill;
            frm.Controls.Add(ctl);
            return frm;
        }

        public AppCategoryEditControl()
        {
            InitializeComponent();
        }

        private AppFrameworkClass _Framework = null;

        public AppFrameworkClass Framework
        {
            get { return _Framework; }
            set { _Framework = value; }
        }


        private void AppCategoryEditControl_Load(object sender, EventArgs e)
        {
            RefreshView();
        }

        public void RefreshView()
        {
            if (this.Framework != null)
            {
                ORMBindingSource bs = new ORMBindingSource(this.Framework.ORMEngine, typeof(AppCategory), this.Framework.Categorys);
                bs.LogItemChange = true;
                this.dgvCategory.DataSource = bs;
                gdvCategory_RowEnter(null, new DataGridViewCellEventArgs(0 , 0 ));
            }
        }

        private bool _gdvCategory_RowEnter = true;
        private void gdvCategory_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (_gdvCategory_RowEnter == false)
            {
                return;
            }
            try
            {
                if (dgvCategory.CurrentRow == null)
                {
                    dgvValue.DataSource = null;
                }
                else
                {
                    AppCategory item = (AppCategory)dgvCategory.Rows[ e.RowIndex ].DataBoundItem  ;
                    if (item != null)
                    {
                        ORMBindingSource bs = ORMBindingSource.CreatePackage(item, typeof(AppCategoryValue), item.Values);
                        bs.LogItemChange = true;
                        dgvValue.DataSource = bs;
                    }
                }
            }
            catch
            {
            }
            finally 
            {
            }
        }

        private void btnNewCategory_Click(object sender, EventArgs e)
        {
            dgvCategory.Rows[dgvCategory.NewRowIndex].Selected = true ; 
        }

        private void btnDeleteCategory_Click(object sender, EventArgs e)
        {
            this._gdvCategory_RowEnter = false;
            //ORMBindingSource bs = (ORMBindingSource)dgvCategory.DataSource;
            //bs.Remove(dgvCategory.CurrentRow.DataBoundItem);
            dgvCategory.Rows.Remove(dgvCategory.CurrentRow);
            this._gdvCategory_RowEnter = true;
        }

    }
}
